package com.yejiali.backend.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yejiali.backend.common.apicode.ResponseCodeEnum;
import com.yejiali.backend.entity.UserFollow;
import com.yejiali.backend.entity.Users;
import com.yejiali.backend.entity.dto.UserDTO;
import com.yejiali.backend.entity.vo.AtUserVO;
import com.yejiali.backend.entity.vo.WeChatUser;
import com.yejiali.backend.mapper.UserFollowMapper;
import com.yejiali.backend.mapper.UsersMapper;
import com.yejiali.backend.response.Response;
import com.yejiali.backend.service.UserFollowService;
import com.yejiali.backend.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 用户表(Users)表服务实现类
 *
 * @author yejiali
 * @since 2025-07-21 15:41:47
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Autowired
    private UserFollowMapper userFollowMapper;
  /**
   * 微信注册
   * @param weChatUser
   */
  @Override
  public Users register(WeChatUser weChatUser) {
      Users user=new Users();
      user.setAvatar(weChatUser.getHeadimgurl());
      user.setNickname(weChatUser.getNickname());
      user.setOpenId(weChatUser.getOpenid());
      user.setGender(weChatUser.getSex());
      user.setLoginTime(DateTime.now());
      save(user);
      return user;
  }

    /**
     * 获取用户详情
     * @param userId
     * @return
     */
    @Override
    public Response<UserDTO> getUsersDetail(Long userId,Long loginUserId) {
        //查询用户详细信息
        Users user = this.getById(userId);
        if(user==null){
            return Response.error(ResponseCodeEnum.NOT_FOUND.getCode(),"用户不存在");
        }
        //封装用户数据
        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
        //查询是否关注该用户
        if(loginUserId!=null){
            QueryWrapper<UserFollow> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",loginUserId);
            queryWrapper.eq("followed_user_id",userId);
            userDTO.setIsFollowed(userFollowMapper.selectCount(queryWrapper) > 0);
        }
        return Response.success(userDTO);
    }

    /**
     * 模糊匹配用户昵称
     * @param keyword
     * @return
     */
    @Override
    public Response<?> searchUsers(String keyword) {
        //模糊查询用户昵称
        List<Users> users = this.lambdaQuery()
                .like(Users::getNickname, keyword)
                .eq(Users::getIsDeleted,0)
                .eq(Users::getAccountStatus,0)
                .list();
        //封装用户(昵称，头像，粉丝)
        List<AtUserVO> atUserVOList = BeanUtil.copyToList(users, AtUserVO.class);
        //查询粉丝量
        for(AtUserVO user:atUserVOList){
            QueryWrapper<UserFollow> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("followed_user_id",user.getUserId());
            user.setFollowCount(userFollowMapper.selectCount(queryWrapper));
        }
        return Response.success(atUserVOList,"查询成功");
    }

    /**
     * 随机推荐的@用户
     */
    @Override
    public Response<?> randomUsers(Long userId) {
        //随机查询10条用户
        List<Users> users = this.lambdaQuery()
                .ne(Users::getUserId,userId)
                .eq(Users::getIsDeleted,0)
                .eq(Users::getAccountStatus,0)
                .orderByDesc(Users::getUserId)
                .last("limit 10")
                .list();
        //封装用户(昵称，头像)
        List<AtUserVO> atUserVOList = BeanUtil.copyToList(users, AtUserVO.class);
        //查询粉丝量
        for(AtUserVO user:atUserVOList){
            QueryWrapper<UserFollow> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("followed_user_id",user.getUserId());
            user.setFollowCount(userFollowMapper.selectCount(queryWrapper));
        }
        return Response.success(atUserVOList,"查询成功");
    }
}


